package comgutianyu.qihangjiaoyu.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import comgutianyu.qihangjiaoyu.R;
import comgutianyu.qihangjiaoyu.model.SpecialClass;
import java.util.List;

public class SpecialClassAdapter extends RecyclerView.Adapter<SpecialClassAdapter.ViewHolder> {
    private List<SpecialClass> specialClasses;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(SpecialClass specialClass);
    }

    public SpecialClassAdapter(List<SpecialClass> specialClasses) {
        this.specialClasses = specialClasses;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_special_class, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SpecialClass specialClass = specialClasses.get(position);
        holder.courseImage.setImageResource(specialClass.getCourseImage());
        holder.courseTitle.setText(specialClass.getTitle());
        holder.studentCount.setText(String.format("共%d节 %d人学习", 
                specialClass.getLessonCount(), specialClass.getStudentCount()));
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(specialClass);
            }
        });
    }

    @Override
    public int getItemCount() {
        return specialClasses.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView courseImage;
        TextView courseTitle;
        TextView studentCount;

        ViewHolder(View view) {
            super(view);
            courseImage = view.findViewById(R.id.courseImage);
            courseTitle = view.findViewById(R.id.courseTitle);
            studentCount = view.findViewById(R.id.studentCount);
        }
    }
} 